﻿using Autofac;
using MahApps.Metro.Controls;
using PLC.Interface.Variables;
using PLCWpfTest.Common;
using PLCWpfTest.Common.Extensions;
using PLCWpfTest.ViewModels;
using System.Windows;
using YWM;

namespace PLCWpfTest.Views
{
    /// <summary>
    /// VariableConfigWindow.xaml 的交互逻辑
    /// </summary>
    public partial class VariableConfigWindow : MetroWindow
    {
        private readonly VariableConfigViewModel _variableConfigViewModel;
        public VariableConfigWindow(VariableConfigViewModel variableConfigViewModel)
        {
            InitializeComponent();
            DataContext = _variableConfigViewModel = variableConfigViewModel;
        }

        private async void AddButton_Click(object sender, RoutedEventArgs e)
        {
            VariableOperationWindow variableOperationWindow = YApp.Resolve<VariableOperationWindow>(new NamedParameter(Constant.DTO_VARIABLE, null));
            if (variableOperationWindow.ShowDialog() == true)
            {
                await _variableConfigViewModel.SearchAsync();
            }
        }

        private async void EditButton_Click(object sender, RoutedEventArgs e)
        {
            if (VariableDataGrid.SelectedValue == null)
            {
                this.ShowWarning("请选择一行编辑");
                return;
            }
            VariableDto variable = (VariableDto)VariableDataGrid.SelectedValue;
            VariableOperationWindow variableOperationWindow = YApp.Resolve<VariableOperationWindow>(new NamedParameter(Constant.DTO_VARIABLE, variable));
            if (variableOperationWindow.ShowDialog() == true)
            {
               await _variableConfigViewModel.SearchAsync();
            }
        }
    }
}
